package com.vcms.controller.maintenance;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto, Pang Pingli
//

import java.awt.Frame;

import com.vcms.controller.customer.TransactionController;
import com.vcms.controller.customer.states.*;
import com.vcms.controller.machine.AccessManager;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.controller.system.MainController;
import com.vcms.entity.store.item.CashStoreItem;
import com.vcms.entity.store.item.DrinksStoreItem;
import com.vcms.entity.store.itemtype.DrinksBrand;
import com.vcms.exception.VMCSException;
import com.vcms.utility.formatter.MoneyFormatter;
import com.vcms.view.simulation.maintenance.MaintenancePanel;
import com.vcms.view.simulation.system.SimulatorControlPanel;
import com.vcms.view.simulation.uicomponent.dialog.MessageDialog;

public class MaintenanceController {

	private MainController mainController;
	private MaintenancePanel maintenancePanel;
	private AccessManager am;

	public MaintenanceController(MainController mctrl) {
		mainController = mctrl;
		am = new AccessManager(this);
	}

	public MainController getMainController() {
		return mainController;
	}

	public MaintenancePanel getMaintenancePanel() {
		return maintenancePanel;
	}

	/**
	 * setup the maintenance panel and display it.
	 */
	public void displayMaintenancePanel() {
		SimulatorControlPanel scp = mainController.getSimulatorControlPanel();
		if (maintenancePanel == null)
			maintenancePanel = new MaintenancePanel((Frame) scp, this);
		maintenancePanel.display();
		maintenancePanel.setActive(MaintenancePanel.DIALOG, true); // setActive of
															// password, invalid
															// and valid
															// display.
	}

	public AccessManager getAccessManager() {
		return am;
	}

	public void loginMaintainer(boolean st) {
		maintenancePanel.displayPasswordState(st);
		maintenancePanel.clearPassword();
		if (st == true) {
			// login successful
			maintenancePanel.setActive(MaintenancePanel.WORKING, true);
			maintenancePanel.setActive(MaintenancePanel.PSWD, false);
			mainController.getMachinaryController().setDoorState(false);
			// sctrl.displayMachineryPanel();
			SimulatorControlPanel scp = mainController.getSimulatorControlPanel();
			scp.setActive(SimulatorControlPanel.ACT_CUSTOMER, false);
			scp.setActive(SimulatorControlPanel.ACT_CUSTOMER_ALPHANUMERIC, false);
			scp.setActive(SimulatorControlPanel.ACT_CUSTOMER_NUMERIC, false);
			
			// Have to terminate transaction in customer panel
			TransactionController transactionController = mainController.getTransactionController();
			transactionController.terminateTransaction();
		}
	}

	// invoked in CoinDisplayListener
	public void displayCoin(int idx) {
		StoreController sctrl = mainController.getStoreController();
		CashStoreItem item;
		try {
			item = (CashStoreItem) sctrl
					.getStoreItem(StoreController.CASH, idx);
			maintenancePanel.getCoinDisplay().displayQty(idx, item.getQuantity());
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.displayCoin:" + e);
		}

	}

	// invoked in DrinkDisplayListener;
	public void displayDrinks(int idx) {
		StoreController sctrl = mainController.getStoreController();
		DrinksStoreItem item;
		try {
			item = (DrinksStoreItem) sctrl.getStoreItem(StoreController.DRINK,
					idx);
			// mapnel.getDrinkDisplay().displayQuantity(idx,
			// item.getQuantity());
			DrinksBrand db = (DrinksBrand) item.getContent();
			maintenancePanel.getDrinksDisplay().displayQty(idx, item.getQuantity());
			maintenancePanel.displayPrice(db.getPrice());
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.displayDrink:" + e);
		}

	}

	// invoked by PriceDisplayListener
	public void setPrice(int pr) {
		StoreController sctrl = mainController.getStoreController();
		// TransactionController tctrl = mCtrl.getTransactionController();

		int curIdx;

		curIdx = maintenancePanel.getCurIdx();
		try {
			sctrl.setPrice(curIdx, pr);
			maintenancePanel.getDrinksDisplay().getPriceDisplay()
					.setValue(MoneyFormatter.toMoney(pr));
			// tctrl.setPrice(curIdx, pr);
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.setPrice:" + e);
		}
	}

	// TotalCashButtonListener
	public void getTotalCash() {
		StoreController sctrl = mainController.getStoreController();

		int tc;

		try {
			tc = sctrl.getTotalCash();
			maintenancePanel.displayTotalCash(tc);

		} catch (VMCSException e) {
			System.out.println("MaintenanceController.getTotalCash:" + e);
		}
	}

	// TransferCashButtonListener
	// get all the cash from store and set store cash 0;
	public void transferAll() {
		int cc; // coin quantity;
		try {
			cc = mainController.getStoreController().transferAll();
			maintenancePanel.displayCoins(cc);
			mainController.getMachinaryController().displayCoinStock();
			// sctrl.updateStoreDisplay(Store.CASH);
			// the cash qty current is displayed in the Maintenance panel needs
			// to be update to be 0;
			// not required.
			maintenancePanel.updateCurrentQtyDisplay(StoreController.CASH, 0);
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.transferAll:" + e);
		}
	}

	// StoreViewerListener, not required;
	public void changeStoreQty(char type, int idx, int qty) {
		@SuppressWarnings("unused")
		StoreController sctrl = mainController.getStoreController();

		try {
			maintenancePanel.updateQtyDisplay(type, idx, qty);
			maintenancePanel.initCollectCash();
			maintenancePanel.initTotalCash();
		} catch (VMCSException e) {
			System.out.println("MaintenanceController.changeStoreQty:" + e);
		}
	}

	// exit button listener;
	public void logoutMaintainer() {
		boolean ds = this.mainController.getMachinaryController().isDoorClosed();
		if (ds == false) {
			@SuppressWarnings("unused")
			MessageDialog msg = new MessageDialog(maintenancePanel, MessageDialog.LOCK_DOOR);
			return;
		}
		maintenancePanel.setActive(MaintenancePanel.DIALOG, true);
		
		// Refresh Customer Panel
		TransactionController transactionController = mainController.getTransactionController();
		transactionController.refreshCustomerPanel();		

	}

	public boolean closeDown() {
		if (maintenancePanel == null)
			return true;
		
		if (!mainController.getMachinaryController().isDoorClosed()) {
			@SuppressWarnings("unused")
			MessageDialog msg = new MessageDialog(maintenancePanel,MessageDialog.LOCK_DOOR);
			return false;
		}
		maintenancePanel.closeDown();
		SimulatorControlPanel smctrl = mainController.getSimulatorControlPanel();
		smctrl.setActive(SimulatorControlPanel.ACT_MAINTAINER, true);
		return true;
	}

}